背景觀念
Producer程式說明
while (true) {
/*produce an item in next produced */
while (counter == BUFFER_SIZE); /* =>表示buffer是額滿狀態*/
/*do nothing */ /* =>額滿就不做任何事*/
buffer[in] = next_produced; /* =>如果沒額滿的話,會產生一個資料,放進buffer array-[in]內,放進去後下一個的位置就是下一行程式所表示的位置*/
in = (in+1) % BUFFER_SIZE; /* =>將資料移到下個位置*/
counter ++; /* =>counter就會增加*/
Consumer程式說明
while (true) {
while (counter == 0 ); /* => 如果buffer=0,就沒有東西可以消耗*/
/do nothing/ /* =>程式等待 /
next_consumed = buffer[out]; / =>如果buffer不等於0的話,便從[out]中讀取一個資料出來*/
out = (out + 1) % BUFFER_SIZE;
counter --; /因為讀取了一個資料,所以要減1/
/*consume the item in next consumed */
Race Condition
Critical Section Problem
Solution to Critical-Section Problem
Critical-Section Handling in OS
Peterson's Solution